package com.zy.sorttedset;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/6 16:17
 **/
public class Test {
    public static void main(String[] args) {
        Set set = new TreeSet(new Comparator(){

            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Per){
                    Per p1 = (Per)o1;
                    if(o2 instanceof Per){
                        Per p2 = (Per)o2;

                        return  p1.getAge()- p2.getAge();
                    }
                }
                return -1;
            }
        });


        Per p1 = new Per("p1",1);
        Per p2= new Per("p2",2);
        Per p3 = new Per("p3",3);
        Per p4 = new Per("p4",-4);
        set.add(p1);
        set.add(p2);
        set.add(p3);
        set.add(p4);
        for (Object p:set
             ) {
            System.out.println(p);
        }
    }
}
